package au.com.handsunited.web.controller.project;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.Project;
import au.com.handsunited.service.EntityService;
import au.com.handsunited.service.ProjectService;

/**
 * @author arunda
 *
 */
public class ProjectListController extends AbstractController {

	private EntityService entityService;
	private ProjectService projectService;
	private String successView;

	private static Logger logger = LoggerFactory.getLogger(ProjectListController.class);


	/**
	 *
	 */
	public ProjectListController() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");
		List<Project> projects = null;

		if (StringUtils.isNotEmpty(id)) {
			BaseEntity entity = entityService.getEntity(id);
			if (entity != null) {
				projects = entity.getProjectsInNetwork();
			}
		} else {
			projects = projectService.getAllProjects();
		}

		ModelAndView mav = new ModelAndView(successView, "projects", projects);

		return mav;
	}

	/**
	 * @param projectService the projectService to set
	 */
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	/**
	 * @param entityService the entityService to set
	 */
	public void setEntityService(EntityService entityService) {
		this.entityService = entityService;
	}

	/**
	 * @param successView the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

}
